package com.example.demo.controller;

import com.example.demo.dao.UserDaoInterface;
import com.example.demo.dao.UserInfoInterface;
import com.example.demo.entity.User;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.UserMapper;
import com.example.demo.util.CollectionResponse;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@Api("controller相关的登录/注册api")
@RestController
@RequestMapping("/Login")
public class LoginController {

    @Autowired
    UserDaoInterface userDao;

    @Autowired
    UserInfoInterface userInfoInterface;


    //登录
    @ApiOperation("显示")
    @ApiImplicitParams({
            @ApiImplicitParam(name="username" ,value="账号",required = true,dataType="String",paramType = "path"),
            @ApiImplicitParam(name="password" ,value="密码",required = true,dataType="String",paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=500,message="登录失败")
    })
    @RequestMapping("/User")
    public List<User> User(){
        List<User> users = userDao.select();
        System.out.println("users:"+users.size());
        return users;
    }

    //登录
    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="username" ,value="账号",required = true,dataType="String",paramType = "path"),
            @ApiImplicitParam(name="password" ,value="密码",required = true,dataType="String",paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=500,message="登录失败")
    })
    @RequestMapping("/Logins")
    public User logins(String username,String password){
        System.out.println("账号："+username);
        System.out.println("密码："+password);
        User user = userDao.Login(username, password);
        if(user != null){
            System.out.println("user:"+user+"user.name:"+user.getName());
            System.out.println("name:"+user.getName());
        }
        return user;
    }

    //修改密码前的发送验证码
    @ApiOperation("发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="string1" ,value="验证码",required = true,dataType="String",paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="发送验证码成功"),
            @ApiResponse(code=500,message="发送验证码失败")
    })
    @RequestMapping("/Email")
    public String Email( String string1)
    {
        String smtphost = "smtp.163.com"; // 发送邮件服务器
        String userid = "zzh5201314tun@163.com"; // 邮件服务器登录用户名
        String password = "HCMKIQOPXUUQMVAV"; // 邮件服务器登录密码
        String from = "zzh5201314tun@163.com"; // 发送人邮件地址
        System.out.println("邮箱："+string1);
        String to=string1;
        String subject = "TT记账验证码"; // 邮件标题
        String body = String.valueOf((int) (Math.random() * 9000) + 1000); // 邮件内容
        String result = "";
        try {
            //初始化Properties类对象
            Properties props = new Properties();
            //设置mail.smtp.host属性
            props.put("mail.smtp.host", smtphost);
            //设置使用验证
            props.put("mail.smtp.auth", "true");
            // 获取非共享的session对象
            Session ssn = Session.getInstance(props, null);
            //创建一个默认的MimeMessage对象。
            MimeMessage message = new MimeMessage(ssn);
            //设置From: 头部的header字段
            message.setFrom(new InternetAddress(from));
            //设置 To: 头部的header字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //设置 Subject: header字段
            message.setSubject(subject);
            // 现在设置的实际消息
            message.setText("验证码："+body+"，若非本人操作，请勿泄露");
            //定义发送协议
            Transport transport = ssn.getTransport("smtp");
            //建立与服务器的链接
            transport.connect(smtphost, userid, password);
            //发送邮件
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
            //transport.send(message);
            //关闭邮件传输
            transport.close();
            //捕获异常
            result = "发送成功";
        } catch (Exception m) {
            result = "发送失败";
            m.printStackTrace();
        }
        System.out.println("结果："+result);
        return body;
    }

    //修改密码
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="string1" ,value="密码",required = true,dataType="String",paramType = "path"),
            @ApiImplicitParam(name="string2" ,value="邮箱",required = true,dataType="String",paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="发送验证码成功"),
            @ApiResponse(code=500,message="发送验证码失败")
    })
    @RequestMapping("/UpdatePass")
    public void UpdatePass(String string1,String string2){
        System.out.println("密码："+string1);
        System.out.println("邮箱："+string2);
        userDao.update(string1,string2);
    }

    //查重
    @RequestMapping("/Check")
    public int Check(String string1,String string2){
        System.out.println("账号："+string1);
        System.out.println("邮箱："+string2);
        User user=userDao.selectNE(string1,string2);
        int i=0;
        if(user!=null){
            i=1;
        }
        System.out.println("i："+i);
        return i;
    }

    //注册
    @RequestMapping("/Register")
    public int Register(User user){
        int i=userDao.insert(user);
        User users = userDao.selectUser(user.getPhone());
        UserInfo userInfo = new UserInfo();
        userInfo.setUserid(users.getId());
        userInfo.setUsername("豚豚");
        userInfo.setClock(1);
        userInfo.setImg("2131165302");
        userInfo.setBudget((float) 0);
        userInfo.setEamil(user.getPhone());
        Date date = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date datetime =  simpleDateFormat.format(date);
        userInfo.setTime(date);

        userInfoInterface.insertUserInfo(userInfo);
        System.out.println("i："+i);
        return i;
    }
}
